package com.aispeech.task.api.security;

import com.aispeech.task.common.data.enums.APICode;
import com.aispeech.task.common.data.vo.response.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        response.setContentType("application/json");
        response.setStatus(APICode.BAD_CREDENTIALS.getCode());

        response.getOutputStream().println(new ObjectMapper().writeValueAsString(Result.fail(APICode.BAD_CREDENTIALS)));
    }
}
